<?php

namespace Budget\BudgetBundle\Helper;


class DateHelper
{
    const FORMAT_DATE_DB    = 'Y-m-d';
    const FORMAT_DB         = 'Y-m-d H:i:s';
    const FORMAT_USER       = 'd.m.Y';
    const FORMAT_YEAR_MONTH = 'Y-m';
    const FORMAT_MONTH_YEAR = 'm.Y';
    const DAY_START         = '00:00:00';
    const DAY_END           = '23:59:59';
    
    /**
     * Convert mm.yyyy to yyyy-mm
     * @param type $date
     * @return type
     */
    public static function convertToYearMonth($date) 
    {
        $tmp = explode('.', $date);
        return $tmp[1].'-'.$tmp[0];
    }
    /**
     * Convert mm.yyyy to yyyy
     * @param type $date
     * @return type
     */
    public static function getYear($date)
    {
        $tmp = explode('.', $date);
        
        return (int)$tmp[1];
    }
    /**
     * Convert mm.yyyy to mm
     * @param type $date
     * @return type
     */
    public static function getMonth($date)
    {
        $tmp = explode('.', $date);
        
        return (int)$tmp[0];
    }
    /**
     * Convert date string to DateTime object
     * @param string mm.yyyy $date
     * @return \DateTime
     */
    public static function createDateObj($dateStr)
    {
        $date = new \DateTime();
        if (!is_null($dateStr)) {
            $date->setDate(self::getYear($dateStr), self::getMonth($dateStr), 1);
        }
        
        return $date;
    }
    // Convert dd.mm.yyyy to yyyy-mm-dd
    public static function convertToFormatDb($date) 
    {
        $tmp = explode('.', $date);
        return $tmp[2].'-'.$tmp[1].'-'.$tmp[0];
    }
    
    /**
     * Return previous month for the given
     * Format mm.yyyy example 01.2014 => 12.2013
     *
     * @param $date month.Year
     * @return string
     */
    public static function getMonthPreview($date) 
    {
        $dateTime = self::createDateObj($date);
        $dateTime->sub(new \DateInterval('P1M'));

        return $dateTime->format(self::FORMAT_MONTH_YEAR);
    }
    
    // Convert number month (1 - January, 12 - December) to name month
    public static function getMonthName($n) 
    {
        $months   = array();
        $months[] = 'January'; 
        $months[] = 'February '; 
        $months[] = 'March'; 
        $months[] = 'April'; 
        $months[] = 'May'; 
        $months[] = 'June';
        $months[] = 'July';
        $months[] = 'August'; 
        $months[] = 'September'; 
        $months[] = 'October'; 
        $months[] = 'November'; 
        $months[] = 'December'; 
//        $months[] = 'Styczeń'; 
//        $months[] = 'Luty'; 
//        $months[] = 'Marzec'; 
//        $months[] = 'Kwiecień'; 
//        $months[] = 'Maj'; 
//        $months[] = 'Czerwiec';
//        $months[] = 'Wrzesień';
//        $months[] = 'Sierpień'; 
//        $months[] = 'Wrzesień'; 
//        $months[] = 'Październik'; 
//        $months[] = 'Listopad'; 
//        $months[] = 'Grudzień'; 
        
        return $months[$n-1];
    }
}

